package com.cp.support;

import org.apache.commons.lang.StringUtils;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.context.ApplicationListener;

/**
 * 提供namespace的server / config 环境空间
 * -profile:为系统配置的环境
 *
 * @author YangXiaoLong
 * 2022/8/5 1:07
 */
public class NacosNameSpaceListener implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {

    @Override
    public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
        String[] profiles = event.getEnvironment().getActiveProfiles();
        String profile = "";
        if (profiles.length > 0) {
            profile = profiles[0];
        }
        if (StringUtils.isNotBlank(profile)) {
            System.setProperty("spring.cloud.nacos.discovery.namespace", profile);
        }
    }
}
